var ui = require('ui'), _ = require('common/util'), ImageView = ui.ImageView;
var font = require('./lib/font');
animationTile = require('./lib/animationTile');
messageBoard = require('./lib/messageBoard');
var app = this;
var gameState = {
    SPLASH: 0,
    MENU: 1,
    MENUSTART: 2,
    MENUOPTIONS: 3,
    MENUINGAME: 4,
    GAMEBREAK: 9,
    GAMEBREAK_IN: 10,
    GAME: 11,
    GAMETURN: 12,
    GAMEOVER: 15
};
var test = 0;
var layer = {
    bgboard: 3 + test,
    board: 4 + test,
    boardships: 5 + test,
    ships: 6 + test,
    shipssmall: 7 + test,
    shoot: 8 + test,
    pointer: 9 + test,
    text: 10 + test,
    gameover: 11 + test,
    menubackground: 15,
    menu: 16,
    menuawalopts: 17,
    menuoptionsopts: 18,
    menustartgameopts: 19,
    menuingameopts: 20,
    splashwhite: 25,
    splash: 26,
    loading: 30
};
var bonus = {
    health: 10
};
var def = {
    shoot: 5,
    wait: 500,
    wait_bol: false
};
_.extend(exports, {
    scene: function(){
        return this.get('scene');
    },
    ':load': function(){
        console.log('Bismillah');
        var self = this;
        var scene = this.scene();
        this.selectedPointer = 0;
        this.selectedShips = 6;
        this.selectedShipsQ = 0;
        scene.setLayers(31);
        this.initSplash();
        this.initMenu();
        this.initGame();
        if (test !== 0) {
            this.setGameState(gameState.GAMEBREAK);
        }
        else {
            this.setGameState(gameState.SPLASH);
        }
    },
    changeTurn: function(OUR, CALCULATE){
        CALCULATE = CALCULATE === false ? false : true;
        this.Game.turn = OUR;
        this.setFrame(this.endikatorObj, OUR === true ? 1 : 0);
        return OUR;
    },
    prepareGame: function(reset_ingame_obj_ships){
        var self = this;
        var scene = this.scene();
        reset_ingame_obj_ships = reset_ingame_obj_ships === true ? true : false;
        if (!reset_ingame_obj_ships) {
            this.boardXY = {
                size: {
                    x: 10,
                    y: 10
                },
                pixelBig: {
                    x: 20,
                    y: 20
                },
                pixelMini: {
                    x: 8,
                    y: 8
                },
                offsetBig: {
                    x: 25,
                    y: 25.5
                },
                offsetMini: {
                    x: 234.4,
                    y: 5.1
                }
            };
            this.newGame();
            delete this.ships;
            this.ships = {
                our: {
                    q: [0, 1, 1, 1, 1, 1],
                    loc: {},
                    health: 0
                },
                enemy: {
                    q: [0, 1, 1, 1, 1, 1],
                    loc: {},
                    health: 0
                }
            };
            this.ships.our.health = this.resetHealthFromQ(this.ships.our.q);
            this.ships.enemy.health = this.resetHealthFromQ(this.ships.enemy.q);
            this.resetShipsLoc();
            delete this.map;
            this.map = {
                our: {},
                enemy: {}
            };
            for (j = 0; j < 2; j++) {
                var arraymap = {};
                for (i = 0; i < 100; i++) {
                    arraymap[i] = 0;
                }
                if (j === 0) {
                    this.map.our[0] = arraymap;
                }
                else {
                    this.map.enemy[0] = arraymap;
                }
            }
            scene.remove(this.bigboardObj, layer.board);
            scene.remove(this.miniboardObj, layer.board);
            this.bigboardObj = scene.add({
                sprite: 'bigboard',
                x: 0,
                y: 0,
                layer: layer.board,
                frame: 0
            });
            this.miniboardObj = scene.add({
                sprite: 'miniboard',
                x: 0,
                y: 0,
                layer: layer.board,
                frame: 0
            });
        }
        this.resetShootsLocObj();
        scene.remove(this.pointerObj[0], layer.pointer);
        this.pointerObj = {};
        this.pointerObj[0] = scene.add({
            sprite: 'pointer',
            x: 321,
            y: 321,
            layer: layer.pointer,
            frame: 0
        });
        this.resetShipsLocObj();
        this.resized();
        this.currentScore.set(0);
        this.currentHealth.set(0);
        this.currentShoot.set(0);
    },
    getScore: function(OUR){
        if (OUR) {
            return this.Game.score;
        }
        else {
            return this.Game.score_enemy;
        }
    },
    addScore: function(OUR, HIT, INTERVAL){
        INTERVAL = INTERVAL === undefined || INTERVAL === null ? this.Game.shoots : INTERVAL;
        VAL = HIT ? (202 - INTERVAL) * 3 : parseInt((202 - INTERVAL) / 5, 10);
        if (OUR) {
            this.Game.score += VAL;
        }
        else {
            this.Game.score_enemy += VAL;
        }
        this.currentScore.set(this.getScore(this.Game.turn));
        return this.getScore(OUR);
    },
    resetHealthFromQ: function(ARRAY){
        var resetHealthFromQ = {
            HEALTH: 0
        };
        _.forEach(ARRAY, function(value, index, array){
            if (value > 0) {
                resetHealthFromQ.HEALTH = resetHealthFromQ.HEALTH + (value * index * bonus.health);
            }
        });
        return resetHealthFromQ.HEALTH;
    },
    newGame: function(reset_newgame){
        this.Game = {
            shoots: 0,
            shoots_remaining: 5,
            turn: true,
            first: true,
            win: false,
            score: 0,
            score_enemy: 0,
            currentSelected: 0
        };
    },
    resetShipsLocObj: function(mini){
        var scene = this.scene();
        var resetShipsLocObj = {
            i: 0,
            ii: 0
        };
        for (resetShipsLocObj.ii = 0; resetShipsLocObj.ii < 5; resetShipsLocObj.ii++) {
            for (resetShipsLocObj.i = 0; resetShipsLocObj.i < 5; resetShipsLocObj.i++) {
                if (mini !== true) {
                    scene.change(this.bigshipsObj[5 - resetShipsLocObj.ii][resetShipsLocObj.i + 1].obj, {
                        x: 321,
                        y: 321
                    });
                }
                if (mini !== false) {
                    scene.change(this.minishipsObj[5 - resetShipsLocObj.ii][resetShipsLocObj.i + 1].obj, {
                        x: 321,
                        y: 321
                    });
                }
            }
        }
    },
    resetShootsLocObj: function(mini){
        var scene = this.scene();
        var resetShootsLocObj = {
            i: 0
        };
        if (mini !== true) {
            for (resetShootsLocObj.i = 0; resetShootsLocObj.i < 100; resetShootsLocObj.i++) {
                if (mini !== true) {
                    scene.change(this.shootObj[resetShootsLocObj.i], {
                        x: 321,
                        y: 321
                    });
                }
                if (mini !== false) {
                    scene.change(this.minishootObj[resetShootsLocObj.i], {
                        x: 321,
                        y: 321
                    });
                }
            }
        }
    },
    setGameState: function(state){
        var self = this;
        var scene = self.scene();
        if (self.state === gameState.SPLASH) {
            scene.animate(layer.splash, {
                x: -320
            });
            scene.animate(layer.splashwhite, {
                x: -320
            });
            setTimeout(function(){
                scene.changeLayer(layer.splash, {
                    visible: false
                });
                scene.changeLayer(layer.splashwhite, {
                    visible: false
                });
            }, 2000);
        }
        if ((self.state === gameState.MENU) && (state === gameState.MENUOPTIONS)) {
            scene.animate(layer.menuawalopts, {
                x: -320,
                duration: 500
            });
            scene.animate(layer.menuoptionsopts, {
                x: self.closed_f(self.dims.menuoptionsopts).x
            });
        }
        else 
            if ((self.state === gameState.MENUOPTIONS) && (state === gameState.MENU)) {
                scene.animate(layer.menuoptionsopts, {
                    x: 320,
                    duration: 500
                });
                scene.animate(layer.menuawalopts, {
                    x: self.closed_f(self.dims.menuawalopts).x
                });
            }
        if ((self.state === gameState.MENU) && (state === gameState.MENUSTART)) {
            scene.animate(layer.menuawalopts, {
                x: -320
            });
            scene.animate(layer.menustartgameopts, {
                x: self.closed_f(self.dims.menustartgameopts).x
            });
        }
        else 
            if ((self.state === gameState.MENUSTART) && (state === gameState.MENU)) {
                scene.animate(layer.menustartgameopts, {
                    x: 320
                });
                scene.animate(layer.menuawalopts, {
                    x: self.closed_f(self.dims.menuawalopts).x
                });
            }
        if ((self.state === gameState.MENU) && (state === gameState.GAMEBREAK)) {
            this.bgtilemenu.stopAnimation();
            scene.animate(layer.menustartgameopts, {
                x: 320
            });
            scene.animate(layer.menu, {
                x: 320
            });
            scene.animate(layer.menuawalopts, {
                x: 320
            });
            scene.animate(layer.menuoptionsopts, {
                x: 320
            });
            setTimeout(function(){
                scene.animate(layer.menubackground, {
                    x: 320,
                    duration: 1000
                });
                setTimeout(function(){
                    scene.changeLayer(layer.menubackground, {
                        x: 321,
                        visible: false
                    });
                }, 1000);
            }, 1);
        }
        if (((self.state === gameState.GAMEOVER) && (state === gameState.MENU)) || ((self.state === gameState.MENUINGAME) && (state === gameState.MENU))) {
            scene.changeLayer(layer.menubackground, {
                visible: true
            });
            scene.animate(layer.menu, {
                x: self.closed_f(self.dims.menu).x
            });
            scene.animate(layer.menuawalopts, {
                x: self.closed_f(self.dims.menuawalopts).x
            });
            scene.animate(layer.menubackground, {
                x: self.closed_f(self.dims.menubackground).x
            });
            self.c("x ", self.closed_f(self.dims.menubackground).x);
            setTimeout(function(){
                self.bgtilemenu.resumeAnimation();
            }, 500);
        }
        if (state === gameState.MENUINGAME) {
            this._state = gameState.GAMEBREAK;
            this.setFrame(this.menuinstartgameObj, 0);
            this.setFrame(this.menuinrestartgameObj, 1);
            this.setFrame(this.menuinexitObj, 1);
            scene.changeLayer(layer.menuingameopts, {
                visible: true
            });
        }
        else {
            scene.changeLayer(layer.menuingameopts, {
                visible: false
            });
        }
        switch (state) {
            case gameState.SPLASH:
                self.state = gameState.SPLASH;
                setTimeout(function(){
                    if (self.state === gameState.SPLASH) {
                        self.setGameState(gameState.MENU);
                    }
                }, 2000);
                break;
            case gameState.MENU:
                self.state = gameState.MENU;
                break;
            case gameState.MENUSTART:
                self.state = gameState.MENUSTART;
                break;
            case gameState.MENUOPTIONS:
                self.state = gameState.MENUOPTIONS;
                break;
            case gameState.GAMEBREAK:
                self.state = gameState.GAMEBREAK;
                this._state = gameState.GAMEBREAK;
                this.changeTurn(true);
                self.prepareGame();
                self.setShipsSelectedByCursor(true);
                break;
            case gameState.GAMEBREAK_IN:
                self.state = gameState.GAMEBREAK_IN;
                this.changeTurn(false);
                self.AI("prepare");
                self.setGameState(gameState.GAME);
                break;
            case gameState.GAME:
                self.state = gameState.GAME;
                this.changeTurn(true, false);
                self.clearBoard();
                self.loadShiptoBoard(true, true);
                self.loadMaptoBoard(false, false);
                self.loadMaptoBoard(true, true);
                this.Game.shoots_remaining = def.shoot;
                this.selectedPointer = this.Game.currentSelected >= 0 && this.Game.currentSelected < 100 ? this.Game.currentSelected : 0;
                this.setPointer(this.selectedPointer);
                this.currentShoot.set(this.Game.shoots_remaining);
                this.currentHealth.set(this.getPointHealth(this.Game.turn));
                this.currentScore.set(this.getScore(this.Game.turn));
                break;
            case gameState.GAMETURN:
                self.state = gameState.GAMETURN;
                this.changeTurn(false);
                self.clearBoard();
                self.loadShiptoBoard(true, false);
                self.loadMaptoBoard(true, false);
                self.loadMaptoBoard(false, true);
                this.Game.shoots_remaining = def.shoot;
                this.c("status game, our", this.getPointHealth(true), "enemy", this.getPointHealth(false));
                this.c("status score, our", this.getScore(true), "enemy", this.getScore(false));
                this.currentShoot.set(this.Game.shoots_remaining);
                this.currentHealth.set(this.getPointHealth(this.Game.turn));
                this.currentScore.set(this.getScore(this.Game.turn));
                self.AI("shoot");
                break;
            case gameState.GAMEOVER:
                self.state = gameState.GAMEOVER;
                self.gameOver(self.Game.win);
                break;
            case gameState.MENUINGAME:
                self.state = gameState.MENUINGAME;
                break;
        }
        this.c("status STATE sekarang :", self.state);
    },
    resized: function(width, height){
        width = width === null || width === undefined ? this.width : width;
        height = height === null || height === undefined ? this.height : height;
        var self = this;
        var scene = this.scene();
        var neededWidth = 30 * 8;
        var x = width - neededWidth;
        scene.changeLayer(layer.background, {
            width: width,
            height: height
        });
        scene.changeLayer(layer.splashwhite, {
            width: width,
            height: height
        });
        if (!this._splashwhite) {
            this._splashwhite = scene.add({
                color: '#fff',
                x: 0,
                y: 0,
                layer: layer.splashwhite,
                width: width,
                height: height
            });
            scene.translate(layer.splash, (width - 210) / 2, (height - 133) / 2);
        }
        var dims, f, fa;
        if (width < height) {
            this.isPortrait = true;
            x /= 2;
            var bot = boardOffsetX;
            dims = {
                bgboard: {
                    x: -(320 / 8),
                    y: 0
                },
                board: {
                    x: 0,
                    y: 0
                },
                endikatorObj: {
                    y: 235,
                    x: 240 - 50 - 10 - 60 - 15
                },
                iconObj: {
                    y: 250,
                    x: 240 - 50 - 10
                },
                bigboardObj: {
                    x: -2.5,
                    y: -2.5
                },
                miniboardObj: {
                    x: 3,
                    y: 233
                },
                menubackground: {
                    x: (width - 138) / 2 - 51,
                    y: height + (neededWidth - (138 * 2)) / 2
                },
                menu: {
                    x: (width - 178) / 2,
                    y: height + (neededWidth - (138 * 2)) / 2
                },
                menuawalopts: {
                    x: (width - 138) / 2,
                    y: height + (neededWidth - 138) / 2 + 100
                },
                menuoptionsopts: {
                    x: (width - 138) / 2,
                    y: height + (neededWidth - 138) / 2 + 100
                },
                menustartgameopts: {
                    x: (width - 138) / 2,
                    y: height + (neededWidth - 138) / 2 + 100
                },
                menuingameopts: {
                    x: (width - 138) / 2,
                    y: height + (neededWidth - 138) / 2 + 100
                },
                offsetMini: {
                    x: 5.1,
                    y: 234.4
                },
                currentScore: {
                    y: 320 - ((320 - 235) / 2) - 2.5 + 15,
                    x: 3 + 84 + 20
                },
                currentHealth: {
                    y: 320 - ((320 - 235) / 2) - 2.5 - 10,
                    x: 3 + 84 + 20
                },
                currentShoot: {
                    y: 320 - ((320 - 235) / 2) - 2.5 - 10,
                    x: 3 + 84 + 20 + 14 + 45
                },
                currentScoreObj: {
                    y: 320 - ((320 - 235) / 2) - 2.5 + 15,
                    x: 3 + 84 + 2
                },
                currentHealthObj: {
                    y: 320 - ((320 - 235) / 2) - 2.5 - 12,
                    x: 3 + 84 + 2
                },
                currentShootObj: {
                    y: 320 - ((320 - 235) / 2) - 2.5 - 11,
                    x: 3 + 84 + 2 + 14 + 45
                }
            };
            f = function(val){
                return {
                    x: val.x,
                    y: val.y - neededWidth
                };
            };
            fa = function(val){
                return {
                    x: val.x,
                    y: val.y
                };
            };
        }
        else {
            dims = {
                bgboard: {
                    x: 0,
                    y: 0
                },
                board: {
                    x: 0,
                    y: 0
                },
                endikatorObj: {
                    x: 320 - ((320 - 235) / 2) - 60 / 2 - 2.5,
                    y: 3 + 84 + 5 + 55
                },
                iconObj: {
                    x: 320 - ((320 - 235) / 2) - 50 / 2 - 2.5,
                    y: 240 - 50 - 10
                },
                bigboardObj: {
                    x: -2.5,
                    y: -2.5
                },
                miniboardObj: {
                    x: 233,
                    y: 3
                },
                menubackground: {
                    x: -(neededWidth - (neededWidth - (320 / 2)) / 2) - 40,
                    y: (height - 138) / 2
                },
                menu: {
                    x: -(neededWidth - (neededWidth - (178 / 2)) / 2),
                    y: (height - 138) / 2
                },
                menuawalopts: {
                    x: -(neededWidth - (neededWidth - (100 / 2)) / 2),
                    y: (height - 138) / 2 + 100
                },
                menuoptionsopts: {
                    x: -(neededWidth - (neededWidth - (100 / 2)) / 2),
                    y: (height - 138) / 2 + 100
                },
                menustartgameopts: {
                    x: -(neededWidth - (neededWidth - (100 / 2)) / 2),
                    y: (height - 138) / 2 + 100
                },
                menuingameopts: {
                    x: -(neededWidth - (neededWidth - (100 / 2)) / 2),
                    y: (height - 138) / 2 + 100
                },
                offsetMini: {
                    x: 234.4,
                    y: 5.1
                },
                currentScore: {
                    x: 320 - ((320 - 235) / 2) - 2.5 - 25,
                    y: 3 + 84 + 10
                },
                currentHealth: {
                    x: 320 - ((320 - 235) / 2) - 2.5 - 25,
                    y: 3 + 84 + 10 + 14 + 10
                },
                currentShoot: {
                    x: 320 - ((320 - 235) / 2) - 2.5 + 30,
                    y: 3 + 84 + 10 + 14 + 10
                },
                currentScoreObj: {
                    x: 320 - ((320 - 235) / 2) - 2.5 - 43,
                    y: 3 + 84 + 10
                },
                currentHealthObj: {
                    x: 320 - ((320 - 235) / 2) - 2.5 - 43,
                    y: 3 + 84 + 10 + 14 + 8
                },
                currentShootObj: {
                    x: 320 - ((320 - 235) / 2) - 2.5 + 12,
                    y: 3 + 84 + 10 + 14 + 9
                }
            };
            f = function(val){
                return {
                    x: val.x + neededWidth,
                    y: val.y
                };
            };
            fa = function(val){
                return {
                    x: val.x,
                    y: val.y
                };
            };
        }
        this.dims = dims;
        this.closed_f = f;
        this.closed_fa = fa;
        if ((self.state === gameState.MENU) || (self.state === gameState.SPLASH) || (self.state === gameState.MENUSTART) || (self.state === gameState.MENUOPTION)) {
            scene.changeLayer(layer.menu, f(dims.menu));
        }
        if ((self.state === gameState.MENU) || (self.state === gameState.SPLASH)) {
            scene.changeLayer(layer.menuawalopts, f(dims.menuawalopts));
        }
        else {
            scene.changeLayer(layer.menuawalopts, {
                x: -320,
                y: f(dims.menuawalopts).y
            });
        }
        if (self.state === gameState.MENUSTART) {
            scene.changeLayer(layer.menustartgameopts, f(dims.menustartgameopts));
        }
        else {
            scene.changeLayer(layer.menustartgameopts, {
                x: 320,
                y: f(dims.menustartgameopts).y
            });
        }
        if (self.state === gameState.MENUOPTIONS) {
            scene.changeLayer(layer.menuoptionsopts, f(dims.menuoptionsopts));
        }
        else {
            scene.changeLayer(layer.menuoptionsopts, {
                x: 320,
                y: f(dims.menuoptionsopts).y
            });
        }
        scene.translate(layer.background, 0, 0);
        scene.changeLayer(layer.menuingameopts, f(dims.menuingameopts));
        scene.changeLayer(layer.bgboard, fa(dims.bgboard));
        scene.changeLayer(layer.board, fa(dims.board));
        scene.change(this.bigboardObj, {
            x: fa(dims.bigboardObj).x,
            y: fa(dims.bigboardObj).y
        });
        scene.change(this.miniboardObj, {
            x: fa(dims.miniboardObj).x,
            y: fa(dims.miniboardObj).y
        });
        scene.change(this.endikatorObj, {
            x: fa(dims.endikatorObj).x,
            y: fa(dims.endikatorObj).y
        });
        scene.change(this.iconObj, {
            x: fa(dims.iconObj).x,
            y: fa(dims.iconObj).y
        });
        this.boardXY.offsetMini = fa(dims.offsetMini);
        scene.change(this.currentScoreObj, {
            x: fa(dims.currentScoreObj).x,
            y: fa(dims.currentScoreObj).y
        });
        scene.change(this.currentHealthObj, {
            x: fa(dims.currentHealthObj).x,
            y: fa(dims.currentHealthObj).y
        });
        scene.change(this.currentShootObj, {
            x: fa(dims.currentShootObj).x,
            y: fa(dims.currentShootObj).y
        });
        this.currentScore.setBase(fa(dims.currentScore).x, fa(dims.currentScore).y);
        this.currentHealth.setBase(fa(dims.currentHealth).x, fa(dims.currentHealth).y);
        this.currentShoot.setBase(fa(dims.currentShoot).x, fa(dims.currentShoot).y);
        this.currentMessage.setPortrait(this.isPortrait);
    },
    ':resized': function(width, height){
        this.width = width;
        this.height = height;
        this.resized();
    },
    menuSelection: function(state){
        var self = this;
        var scene = this.scene();
        switch (self.state) {
            case gameState.MENU:
                scene.change(this.menunewObj, {
                    frame: state === 0 ? 0 : 1
                });
                scene.change(this.menuhowtoplayObj, {
                    frame: state === 1 ? 0 : 1
                });
                scene.change(this.menuhighscoreObj, {
                    frame: state === 2 ? 0 : 1
                });
                scene.change(this.menucreditObj, {
                    frame: state === 3 ? 0 : 1
                });
                break;
            case gameState.MENUSTART:
                scene.change(this.menuadventureObj, {
                    frame: state === 0 ? 0 : 1
                });
                scene.change(this.menuquickplayObj, {
                    frame: state === 1 ? 0 : 1
                });
                scene.change(this.menuback_startgameObj, {
                    frame: state === 2 ? 0 : 1
                });
                break;
            case gameState.MENUOPTIONS:
                scene.change(this.menudeck1Obj, {
                    frame: state === 0 ? 0 : 1
                });
                scene.change(this.menudeck2Obj, {
                    frame: state === 1 ? 0 : 1
                });
                scene.change(this.menudeck3Obj, {
                    frame: state === 2 ? 0 : 1
                });
                scene.change(this.menuback_optionsObj, {
                    frame: state === 3 ? 0 : 1
                });
                break;
            case gameState.MENUINGAME:
                scene.change(this.menuinstartgameObj, {
                    frame: state === 0 ? 0 : 1
                });
                scene.change(this.menuinrestartgameObj, {
                    frame: state === 1 ? 0 : 1
                });
                scene.change(this.menuinexitObj, {
                    frame: state === 2 ? 0 : 1
                });
                break;
        }
        this._selection = state;
    },
    ':keypress': function(key){
        var self = this;
        var scene = this.scene();
        var count = 4;
        console.log(key);
        var KEYPRESSED = {
            GAME: {
                FIRE: {}
            }
        };
        key = key === '50' ? 'up' : key === '52' ? 'left' : key === '54' ? 'right' : key === '56' ? 'down' : key === '53' ? 'fire' : key;
        if (key === "back") {
            console.log(back);
        }
        if (self.state === gameState.SPLASH) {
            if (key === 'fire') {
                self.setGameState(gameState.MENU);
            }
            return;
        }
        if (self.state === gameState.MENU) {
            count = 4;
            if ((key === 'up') || (key === 'left')) {
                self.menuSelection(this._selection > 0 ? this._selection - 1 : (count - 1));
            }
            if ((key === 'down') || (key === 'right')) {
                self.menuSelection(this._selection < (count - 1) ? this._selection + 1 : 0);
            }
            if (key === 'fire') {
                if (self._selection === 0) {
                    this.setGameState(gameState.GAMEBREAK);
                    self.menuSelection(0);
                    return;
                }
                else 
                    if (self._selection === 1) {
                        app.pushView('howtoplay');
                        self.menuSelection(1);
                        return;
                    }
                    else 
                        if (self._selection === 2) {
                            app.pushView('highscore');
                            self.menuSelection(2);
                            return;
                        }
                        else 
                            if (self._selection === 3) {
                                app.pushView('credit');
                                self.menuSelection(3);
                                return;
                            }
            }
            return;
        }
        if (self.state === gameState.MENUSTART) {
            count = 3;
            if ((key === 'up') || (key === 'left')) {
                self.menuSelection(this._selection > 0 ? this._selection - 1 : (count - 1));
            }
            if ((key === 'down') || (key === 'right')) {
                self.menuSelection(this._selection < (count - 1) ? this._selection + 1 : 0);
            }
            if (key === 'fire') {
                if (self._selection === 2) {
                    this.setGameState(gameState.MENU);
                    self.menuSelection(0);
                    return;
                }
                else 
                    if (self._selection === 0) {
                        this.setGameState(gameState.GAMEBREAK);
                        self.menuSelection(0);
                        return;
                    }
            }
            return;
        }
        if (self.state === gameState.MENUOPTIONS) {
            count = 4;
            if ((key === 'up') || (key === 'left')) {
                self.menuSelection(this._selection > 0 ? this._selection - 1 : (count - 1));
            }
            if ((key === 'down') || (key === 'right')) {
                self.menuSelection(this._selection < (count - 1) ? this._selection + 1 : 0);
            }
            if (key === 'fire') {
                if (self._selection === 3) {
                    this.setGameState(gameState.MENU);
                    self.menuSelection(2);
                    return;
                }
            }
            return;
        }
        if (self.state === gameState.MENUINGAME) {
            count = 3;
            if ((key === 'up') || (key === 'left')) {
                self.menuSelection(this._selection > 0 ? this._selection - 1 : (count - 1));
            }
            if ((key === 'down') || (key === 'right')) {
                self.menuSelection(this._selection < (count - 1) ? this._selection + 1 : 0);
            }
            if (key === 'fire') {
                if (self._selection === 0) {
                    this.setGameState(gameState.GAMEBREAK_IN);
                    self.menuSelection(0);
                    return;
                }
                if (self._selection === 1) {
                    this.setGameState(gameState.GAMEBREAK);
                    self.menuSelection(0);
                    return;
                }
                if (self._selection === 2) {
                    this.setGameState(gameState.MENU);
                    self.menuSelection(0);
                    return;
                }
            }
            return;
        }
        if (self.state === gameState.GAMEBREAK) {
            if (key === 'fire') {
                if (!this.isShipsPlacedinMap(true, 0, this.selectedPointer, this.selectedObjectOffset)) {
                    if (this.setShipsSelectedByCursor(false, true, true)) {
                        return self.setGameState(gameState.MENUINGAME);
                    }
                }
            }
            if (key === '48') {
                if (!this.isInvalidselectBoard(this.selectedPointer, temp = {
                    x: this.selectedObjectOffset.y,
                    y: this.selectedObjectOffset.x
                })) {
                    this.selectedObjectOffset = {
                        x: this.selectedObjectOffset.y,
                        y: this.selectedObjectOffset.x
                    };
                    this.selectedPointer = this.selectBoard(this.selectedObject, this.selectedPointer, 0, this.selectedObjectOffset, this.boardXY.size, this.boardXY.pixelBig, this.boardXY.offsetBig);
                    this.selectedObjectRotate = this.selectedObjectRotate === 0 ? 1 : 0;
                    this.setFrame(this.selectedObject, this.selectedObjectRotate);
                    this.setFrame(this.selectedObjectMini, this.selectedObjectRotate);
                    this.selectBoard(this.selectedObjectMini, this.selectedPointer, 0, this.selectedObjectOffset, this.boardXY.size, this.boardXY.pixelMini, this.boardXY.offsetMini);
                }
            }
            if (key === 'left') {
                this.selectBoard(this.selectedObjectMini, this.selectedPointer, -1, this.selectedObjectOffset, this.boardXY.size, this.boardXY.pixelMini, this.boardXY.offsetMini);
                this.selectedPointer = this.selectBoard(this.selectedObject, this.selectedPointer, -1, this.selectedObjectOffset, this.boardXY.size, this.boardXY.pixelBig, this.boardXY.offsetBig);
            }
            if (key === 'up') {
                this.selectBoard(this.selectedObjectMini, this.selectedPointer, -10, this.selectedObjectOffset, this.boardXY.size, this.boardXY.pixelMini, this.boardXY.offsetMini);
                this.selectedPointer = this.selectBoard(this.selectedObject, this.selectedPointer, -10, this.selectedObjectOffset, this.boardXY.size, this.boardXY.pixelBig, this.boardXY.offsetBig);
            }
            if (key === 'right') {
                this.selectBoard(this.selectedObjectMini, this.selectedPointer, 1, this.selectedObjectOffset, this.boardXY.size, this.boardXY.pixelMini, this.boardXY.offsetMini);
                this.selectedPointer = this.selectBoard(this.selectedObject, this.selectedPointer, 1, this.selectedObjectOffset, this.boardXY.size, this.boardXY.pixelBig, this.boardXY.offsetBig);
            }
            if (key === 'down') {
                this.selectBoard(this.selectedObjectMini, this.selectedPointer, 10, this.selectedObjectOffset, this.boardXY.size, this.boardXY.pixelMini, this.boardXY.offsetMini);
                this.selectedPointer = this.selectBoard(this.selectedObject, this.selectedPointer, 10, this.selectedObjectOffset, this.boardXY.size, this.boardXY.pixelBig, this.boardXY.offsetBig);
            }
            this.setPointer(this.selectedPointer);
            return;
        }
        if (self.state === gameState.GAMEBREAK_IN) {
            if (key === 'fire') {
                self.setGameState(gameState.GAME);
            }
            if (key === 'left') {
                this.selectedPointer = this.setPointer(this.selectedPointer - 1);
            }
            if (key === 'up') {
                this.selectedPointer = this.setPointer(this.selectedPointer - 10);
            }
            if (key === 'right') {
                this.selectedPointer = this.setPointer(this.selectedPointer + 1);
            }
            if (key === 'down') {
                this.selectedPointer = this.setPointer(this.selectedPointer + 10);
            }
            return;
        }
        if (self.state === gameState.GAME) {
            if (key === 'fire') {
                this.Game.currentSelected = this.selectedPointer;
                KEYPRESSED.GAME.FIRE = this.ShootIt(false, this.selectedPointer);
                if (this.CheckGame().END) {
                    return;
                }
                if (KEYPRESSED.GAME.FIRE.SUCCESS) {
                    if (KEYPRESSED.GAME.FIRE.NEXT) {
                        self.setGameState(gameState.GAMETURN);
                    }
                }
            }
            if (key === 'left') {
                this.selectedPointer = this.setPointer(this.selectedPointer - 1);
            }
            if (key === 'up') {
                this.selectedPointer = this.setPointer(this.selectedPointer - 10);
            }
            if (key === 'right') {
                this.selectedPointer = this.setPointer(this.selectedPointer + 1);
            }
            if (key === 'down') {
                this.selectedPointer = this.setPointer(this.selectedPointer + 10);
            }
            return;
        }
        if (self.state === gameState.GAMETURN) {
            if (key === 'fire') {
                if (!def.wait_bol) {
                    self.setGameState(gameState.GAME);
                }
            }
            if (key === 'left') {
                this.selectedPointer = this.setPointer(this.selectedPointer - 1);
            }
            if (key === 'up') {
                this.selectedPointer = this.setPointer(this.selectedPointer - 10);
            }
            if (key === 'right') {
                this.selectedPointer = this.setPointer(this.selectedPointer + 1);
            }
            if (key === 'down') {
                this.selectedPointer = this.setPointer(this.selectedPointer + 10);
            }
            return;
        }
    },
    CheckGame: function(){
        var CheckGame = {
            END: false
        };
        if (this.getPointHealth(false) === 0) {
            CheckGame.END = true;
            this.Game.win = true;
            this.setGameState(gameState.GAMEOVER);
        }
        else 
            if (this.getPointHealth(true) === 0) {
                CheckGame.END = true;
                this.Game.win = false;
                this.setGameState(gameState.GAMEOVER);
            }
        return CheckGame;
    },
    DrawX: function(OBJECT_X, INDEX, MINI){
        var DrawX = {
            OBJECT_X: OBJECT_X,
            INDEX: INDEX,
            MINI: MINI
        };
        if (DrawX.MINI !== true) {
            this.selectBoard(DrawX.OBJECT_X, DrawX.INDEX, 0);
        }
        if (DrawX.MINI !== false) {
            this.selectBoard(DrawX.OBJECT_X, DrawX.INDEX, 0, OFFSET = {
                x: 1,
                y: 1
            }, this.boardXY.size, this.boardXY.pixelMini, this.boardXY.offsetMini);
        }
    },
    ShootIt: function(OUR, INDEX, IS_X, IS_SAVED){
        var ShootIt = {
            OUR: OUR === false ? false : true,
            INDEX: INDEX === null || INDEX === undefined || INDEX < 0 || INDEX > 99 ? 0 : INDEX,
            IS_SAVE: IS_SAVE === false ? false : true,
            IS_X: IS_X === false ? false : true,
            DATA: {
                NEXT: false,
                SUCCESS: false,
                HIT: false
            },
            DATA_SHIP: {}
        };
        if (!this.getMapPlaced(ShootIt.OUR, 0, ShootIt.INDEX).SHOOT) {
            ShootIt.DATA_SHIP = this.getShipsPlacedinMap(ShootIt.OUR, 0, ShootIt.INDEX);
            if (ShootIt.IS_X) {
                this.DrawX(this.shootObj[ShootIt.INDEX], ShootIt.INDEX, false);
                this.setFrame(this.shootObj[ShootIt.INDEX], 0);
                this.Game.shoots++;
            }
            if (ShootIt.IS_SAVE) {
                this.setMapPlaced(ShootIt.OUR, 0, ShootIt.INDEX, 1);
            }
            if (ShootIt.DATA_SHIP.is_ships) {
                if (ShootIt.IS_SAVE) {
                    this.setMapPlaced(ShootIt.OUR, 0, ShootIt.INDEX, 2);
                }
                this.setPointHealth(ShootIt.OUR, -1 * bonus.health);
                ShootIt.DATA.HIT = true;
                this.setFrame(this.shootObj[ShootIt.INDEX], 1);
                this.setShipsShooted(ShootIt.OUR, ShootIt.DATA_SHIP.ships.ships, ShootIt.DATA_SHIP.ships.ships_num, 1);
            }
            this.Game.shoots_remaining--;
            if (this.Game.shoots_remaining <= 0) {
                ShootIt.DATA.NEXT = true;
            }
            this.currentShoot.set(this.Game.shoots_remaining);
            this.addScore(this.Game.turn, ShootIt.DATA_SHIP.is_ships);
            ShootIt.DATA.SUCCESS = true;
        }
        else {
            ShootIt.DATA.SUCCESS = false;
        }
        return ShootIt.DATA;
    },
    setPointHealth: function(OUR, VAL, RESET){
        if (OUR) {
            if (RESET === true) {
                this.ships.our.health = VAL;
                return VAL;
            }
            this.ships.our.health = this.ships.our.health + VAL;
            this.currentHealth.set(this.getPointHealth(this.Game.turn));
            return this.ships.our.health;
        }
        else {
            if (RESET === true) {
                this.ships.enemy.health = VAL;
                return VAL;
            }
            this.ships.enemy.health = this.ships.enemy.health + VAL;
            this.currentHealth.set(this.getPointHealth(this.Game.turn));
            return this.ships.enemy.health;
        }
    },
    getPointHealth: function(OUR){
        if (OUR) {
            return this.ships.our.health;
        }
        else {
            return this.ships.enemy.health;
        }
    },
    AI: function(mov){
        var self = this;
        var scene = this.scene();
        def.wait_bol = true;
        switch (mov) {
            case "shoot":
                var AI = {
                    GAME: {
                        FIRE: {
                            SUCCESS: false,
                            NEXT: false
                        }
                    }
                };
                while (!AI.GAME.FIRE.NEXT) {
                    var AItempIndex = this.generateEnemyShoot();
                    this.selectedPointer = AItempIndex;
                    this.setPointer(this.selectedPointer);
                    AI.GAME.FIRE = this.ShootIt(true, AItempIndex);
                    if (this.CheckGame().END) {
                        return;
                    }
                    if (AI.GAME.FIRE.SUCCESS) {
                        if (AI.GAME.FIRE.NEXT) {
                            this.c("AI telah selesai menembak");
                        }
                    }
                }
                break;
            case "prepare":
                this.generateEnemyLoc();
                break;
        }
        def.wait_bol = false;
        return;
    },
    generateEnemyShoot: function(){
        var generateEnemyShoot = {
            index: 0,
            tidak_bisa: false
        };
        do {
            generateEnemyShoot.index = Math.round(Math.random() * 100);
            generateEnemyShoot.tidak_bisa = this.getMapPlaced(true, 0, generateEnemyShoot.index).SHOOT ? true : false;
        }
        while (generateEnemyShoot.tidak_bisa === true);
        return generateEnemyShoot.index;
    },
    generateEnemyLoc: function(){
        var generateEnemyLoc_index = -1;
        var generateEnemyLoc_rotate = 0;
        var generateEnemyLoc_tidak_bisa = false;
        var generateEnemyLoc_Offset = {
            x: 1,
            y: 1
        };
        for (generateEnemyLoc_ii = 0; generateEnemyLoc_ii < 5; generateEnemyLoc_ii++) {
            for (generateEnemyLoc_i = 0; generateEnemyLoc_i < this.ships.enemy.q[5 - generateEnemyLoc_ii]; generateEnemyLoc_i++) {
                generateEnemyLoc_tidak_bisa = false;
                do {
                    generateEnemyLoc_index = Math.round(Math.random() * 100);
                    generateEnemyLoc_rotate = Math.round(Math.random());
                    if (generateEnemyLoc_rotate === 1) {
                        generateEnemyLoc_Offset = {
                            x: this.bigshipsObj[5 - generateEnemyLoc_ii][generateEnemyLoc_i + 1].offset.y,
                            y: this.bigshipsObj[5 - generateEnemyLoc_ii][generateEnemyLoc_i + 1].offset.x
                        };
                    }
                    else {
                        generateEnemyLoc_Offset = {
                            x: this.bigshipsObj[5 - generateEnemyLoc_ii][generateEnemyLoc_i + 1].offset.x,
                            y: this.bigshipsObj[5 - generateEnemyLoc_ii][generateEnemyLoc_i + 1].offset.y
                        };
                    }
                    generateEnemyLoc_tidak_bisa = this.isInvalidselectBoard(generateEnemyLoc_index, generateEnemyLoc_Offset) ? true : this.isShipsPlacedinMap(false, 0, generateEnemyLoc_index, generateEnemyLoc_Offset) ? true : false;
                }
                while (generateEnemyLoc_tidak_bisa === true);
                this.setShipsPlaced(false, 0, (5 - generateEnemyLoc_ii), (generateEnemyLoc_i + 1), generateEnemyLoc_index, generateEnemyLoc_rotate);
            }
        }
        return this.ships.enemy.loc;
    },
    clearBoard: function(maporships, mini){
        if (maporships !== false) {
            this.resetShootsLocObj(mini);
        }
        if (maporships !== true) {
            this.resetShipsLocObj(mini);
        }
    },
    loadShiptoBoard: function(our, mini, map){
        map = 0;
        our = our === false ? false : true;
        var loadShiptoBoard_i = 0;
        var loadShiptoBoard_ii = 0;
        for (loadShiptoBoard_ii = 0; loadShiptoBoard_ii < 5; loadShiptoBoard_ii++) {
            for (loadShiptoBoard_i = 0; loadShiptoBoard_i < 5; loadShiptoBoard_i++) {
                this.selectedObject = this.bigshipsObj[5 - loadShiptoBoard_ii][loadShiptoBoard_i + 1].obj;
                this.selectedObjectMini = this.minishipsObj[5 - loadShiptoBoard_ii][loadShiptoBoard_i + 1].obj;
                this.selectedPointer = this.getShipsPlaced(our, 5 - loadShiptoBoard_ii, loadShiptoBoard_i + 1).index;
                this.selectedObjectRotate = this.getShipsPlaced(our, 5 - loadShiptoBoard_ii, loadShiptoBoard_i + 1).rotate;
                this.selectedPointer = this.selectedPointer < 0 || this.selectedPointer > 99 ? -99 : this.selectedPointer;
                if (this.selectedObjectRotate === 1) {
                    this.selectedObjectOffset = {
                        y: this.bigshipsObj[this.selectedShips][1].offset.x,
                        x: this.bigshipsObj[this.selectedShips][1].offset.y
                    };
                }
                else {
                    this.selectedObjectOffset = {
                        x: this.bigshipsObj[this.selectedShips][1].offset.x,
                        y: this.bigshipsObj[this.selectedShips][1].offset.y
                    };
                }
                if (mini !== false) {
                    this.setFrame(this.selectedObjectMini, this.selectedObjectRotate);
                    this.selectBoard(this.selectedObjectMini, this.selectedPointer, 0, this.selectedObjectOffset, this.boardXY.size, this.boardXY.pixelMini, this.boardXY.offsetMini);
                }
                if (mini !== true) {
                    this.setFrame(this.selectedObject, this.selectedObjectRotate);
                    this.selectBoard(this.selectedObject, this.selectedPointer, 0, this.selectedObjectOffset, this.boardXY.size, this.boardXY.pixelBig, this.boardXY.offsetBig);
                }
            }
        }
        this.selectedPointer = this.setPointer(0);
    },
    loadMaptoBoard: function(our, mini, map){
        map = 0;
        our = our === false ? false : true;
        var test_x = "";
        var test_x2 = "";
        var loadMaptoBoard_i = 0;
        for (loadMaptoBoard_i = 0; loadMaptoBoard_i < 100; loadMaptoBoard_i++) {
            this.selectedPointer = this.getMapPlaced(our, map, loadMaptoBoard_i).SHOOT ? loadMaptoBoard_i : -99;
            if (mini !== false) {
                this.selectedObjectMini = this.minishootObj[loadMaptoBoard_i];
                this.setFrame(this.selectedObjectMini, this.getMapPlaced(our, map, loadMaptoBoard_i).HIT === true ? 1 : 0);
                this.selectBoard(this.selectedObjectMini, this.selectedPointer, 0, null, this.boardXY.size, this.boardXY.pixelMini, this.boardXY.offsetMini);
            }
            if (mini !== true) {
                this.selectedObject = this.shootObj[loadMaptoBoard_i];
                this.setFrame(this.selectedObject, this.getMapPlaced(our, map, loadMaptoBoard_i).HIT === true ? 1 : 0);
                this.selectBoard(this.selectedObject, this.selectedPointer);
            }
        }
    },
    setShipsSelectedByCursor: function(change, reset, savelast){
        var self = this;
        var scene = this.scene();
        var status = false;
        if (savelast) {
            this.setShipsPlaced(true, 0, this.selectedShips, this.selectedShipsQ, this.selectedPointer, this.selectedObjectRotate);
        }
        status = this.selectedShipsQ > 1 ? false : this.selectedShips <= 1 ? this.selectedShipsQ <= 1 ? true : false : false;
        if ((status) && (!change)) {
            return status;
        }
        this.selectedShips = this.selectedShipsQ > 1 ? this.selectedShips : this.selectedShips <= 1 ? 5 : this.selectedShips - 1;
        this.selectedShipsQ = this.selectedShipsQ <= 1 ? this.ships.our.q[this.selectedShips] : this.selectedShipsQ - 1;
        this.selectedObject = this.bigshipsObj[this.selectedShips][this.selectedShipsQ].obj;
        this.selectedObjectMini = this.minishipsObj[this.selectedShips][this.selectedShipsQ].obj;
        var ships = this.getShipsPlaced(true, this.selectedShips, this.selectedShipsQ);
        if (((reset !== undefined) && (reset === false)) || ((ships.index >= 0) && (ships.index <= 99))) {
            this.selectedObjectRotate = ships.rotate;
            this.selectedPointer = ships.index;
            this.setShipsPlaced(true, 0, this.selectedShips, this.selectedShipsQ, -1, this.selectedObjectRotate);
        }
        else {
            this.selectedObjectRotate = 0;
            this.selectedPointer = 0;
        }
        if (this.selectedObjectRotate === 1) {
            this.selectedObjectOffset = {
                y: this.bigshipsObj[this.selectedShips][1].offset.x,
                x: this.bigshipsObj[this.selectedShips][1].offset.y
            };
        }
        else {
            this.selectedObjectOffset = {
                x: this.bigshipsObj[this.selectedShips][1].offset.x,
                y: this.bigshipsObj[this.selectedShips][1].offset.y
            };
        }
        this.setFrame(this.selectedObject, this.selectedObjectRotate);
        this.setFrame(this.selectedObjectMini, this.selectedObjectRotate);
        this.selectedPointer = this.selectBoard(this.selectedObject, this.selectedPointer, 0, this.selectedObjectOffset, this.boardXY.size, this.boardXY.pixelBig, this.boardXY.offsetBig);
        this.selectBoard(this.selectedObjectMini, this.selectedPointer, 0, this.selectedObjectOffset, this.boardXY.size, this.boardXY.pixelMini, this.boardXY.offsetMini);
        this.setPointer(this.selectedPointer);
        return status;
    },
    setPointer: function(index){
        index = index > 99 ? 99 : index < 0 ? 0 : index;
        var temp_index = index === null || index === undefined ? 0 : index;
        temp_index = this.selectBoard(this.pointerObj[0], index);
        return temp_index;
    },
    setMapPlaced: function(our, map, index, val){
        map = 0;
        val = val !== 1 && val !== 2 ? val === true ? 1 : 0 : val;
        index = index < 0 ? 0 : index > 99 ? 0 : index;
        if (our) {
            this.map.our[map][index] = val;
        }
        else {
            this.map.enemy[map][index] = val;
        }
        return;
    },
    getMapPlaced: function(our, map, index){
        map = 0;
        var getMapPlaced = {
            SHOOT: false,
            HIT: false,
            VAL: 0
        };
        index = index < 0 ? 0 : index > 99 ? 0 : index;
        if (our) {
            if (this.map.our[map][index] === 2) {
                getMapPlaced.SHOOT = true;
                getMapPlaced.HIT = true;
            }
            else 
                if (this.map.our[map][index] === 1) {
                    getMapPlaced.SHOOT = true;
                }
            getMapPlaced.VAL = this.map.our[map][index];
        }
        else {
            if (this.map.enemy[map][index] === 2) {
                getMapPlaced.SHOOT = true;
                getMapPlaced.HIT = true;
            }
            else 
                if (this.map.enemy[map][index] === 1) {
                    getMapPlaced.SHOOT = true;
                }
            getMapPlaced.VAL = this.map.enemy[map][index];
        }
        return getMapPlaced;
    },
    isMapHitted: function(OUR, MAP, INDEX){
        return this.getMapPlaced(OUR, MAP, INDEX).HIT;
    },
    isMapShooted: function(OUR, MAP, INDEX){
        return this.getMapPlaced(OUR, MAP, INDEX).SHOOT;
    },
    setShipsShooted: function(our, ship, ship_num, val){
        if (our) {
            this.ships.our.loc[ship][ship_num].shoot = val;
            return this.ships.our.loc[ship][ship_num];
        }
        else {
            this.ships.enemy.loc[ship][ship_num].shoot = val;
            return this.ships.enemy.loc[ship][ship_num];
        }
    },
    setShipsPlaced: function(our, map, ship, ship_num, ind, rot){
        map = 0;
        if (our) {
            this.ships.our.loc[ship][ship_num].index = ind;
            this.ships.our.loc[ship][ship_num].rotate = rot;
            this.ships.our.loc[ship][ship_num].map = map;
            return this.ships.our.loc[ship][ship_num];
        }
        else {
            this.ships.enemy.loc[ship][ship_num].index = ind;
            this.ships.enemy.loc[ship][ship_num].rotate = rot;
            this.ships.enemy.loc[ship][ship_num].map = map;
            return this.ships.enemy.loc[ship][ship_num];
        }
    },
    getShipsPlaced: function(our, ship, ship_num){
        if (our) {
            return this.ships.our.loc[ship][ship_num];
        }
        else {
            return this.ships.enemy.loc[ship][ship_num];
        }
    },
    setShipsQ: function(our, ship, q){
        var IS_RESET = q === true ? true : false;
        q = q === undefined || q === null ? 1 : q;
        if (_.isArray(ship)) {
            if (our) {
                this.ships.our.q = ship;
            }
            else {
                this.ships.enemy.q = ship;
            }
        }
        else {
            if (our) {
                this.ships.our.q[ship] = q;
            }
            else {
                this.ships.enemy.q[ship] = q;
            }
        }
        if (IS_RESET) {
            return this.resetShipsLoc(our);
        }
    },
    getShipsQ: function(our, ship){
        our = our === false ? false : true;
        if (our) {
            return this.ships.our.q[ship];
        }
        else {
            return this.ships.enemy.q[ship];
        }
    },
    resetShipsLoc: function(our){
        var arrayships = {};
        var arrayships_e = {};
        var resetShipsLoc = {
            i: 0,
            ii: 0
        };
        for (resetShipsLoc.ii = 0; resetShipsLoc.ii < 5; resetShipsLoc.ii++) {
            if (our !== false) {
                var arrayshipsin = {};
                for (resetShipsLoc.i = 0; resetShipsLoc.i < this.ships.our.q[5 - resetShipsLoc.ii]; resetShipsLoc.i++) {
                    arrayshipsin[resetShipsLoc.i + 1] = {
                        index: -1,
                        rotate: 0,
                        map: 0,
                        shoot: 0
                    };
                }
                arrayships[5 - resetShipsLoc.ii] = arrayshipsin;
            }
            if (our !== true) {
                var arrayshipsin_e = {};
                for (resetShipsLoc.i = 0; resetShipsLoc.i < this.ships.enemy.q[5 - resetShipsLoc.ii]; resetShipsLoc.i++) {
                    arrayshipsin_e[resetShipsLoc.i + 1] = {
                        index: -1,
                        rotate: 0,
                        map: 0,
                        shoot: 0
                    };
                }
                arrayships_e[5 - resetShipsLoc.ii] = arrayshipsin_e;
            }
        }
        if (our !== false) {
            delete this.ships.our.loc;
            this.ships.our.loc = arrayships;
        }
        if (our !== true) {
            delete this.ships.enemy.loc;
            this.ships.enemy.loc = arrayships_e;
        }
        if (our === false) {
            return arrayships_e;
        }
        else {
            return arrayships;
        }
    },
    getShipsPlacedinMap: function(our, map, index, offset, rotate, load){
        map = 0;
        var IS_ATTACKED = this.getMapPlaced(our, map, index).SHOOT;
        var IS_SHIPS = false;
        var SHIPS = {
            ships: 0,
            ships_num: 0,
            offset: {
                x: 0,
                y: 0
            }
        };
        var INX = -1;
        var data;
        if (load) {
            data = {
                is_attacked: IS_ATTACKED
            };
            return data;
        }
        var IS_ROTATE = rotate === undefined ? false : true;
        var ROTATE = IS_ROTATE ? rotate === 1 ? true : false : false;
        var FOR_OUR = our ? this.ships.our.q : this.ships.enemy.q;
        var RANGE = {
            x: 1,
            y: 1
        };
        if (offset.x !== undefined) {
            RANGE.x = offset.x;
        }
        if (offset.y !== undefined) {
            RANGE.y = offset.y;
        }
        if (ROTATE) {
            RANGE = {
                x: RANGE.y,
                y: RANGE.x
            };
        }
        for (ii = 0; ii < 5; ii++) {
            for (i = 1; i <= FOR_OUR[5 - ii]; i++) {
                var ats = this.getShipsPlaced(our, 5 - ii, i);
                if ((ats.index >= 0) && (ats.index < 100)) {
                    for (iix = 1; iix <= RANGE.x; iix++) {
                        for (iiy = 1; iiy <= RANGE.y; iiy++) {
                            var tmp = {
                                x: this.bigshipsObj[5 - ii][i].offset.x,
                                y: this.bigshipsObj[5 - ii][i].offset.y
                            };
                            tmp = ats.rotate === 0 ? {
                                x: tmp.x,
                                y: tmp.y
                            } : {
                                x: tmp.y,
                                y: tmp.x
                            };
                            for (iisx = 1; iisx <= tmp.x; iisx++) {
                                for (iisy = 1; iisy <= tmp.y; iisy++) {
                                    if ((index + (iix - 1) + ((iiy - 1) * 10)) === (ats.index + (iisx - 1) + ((iisy - 1) * 10))) {
                                        IS_SHIPS = true;
                                        SHIPS = {
                                            ships: 5 - ii,
                                            ships_num: i,
                                            offset: {
                                                x: iisx,
                                                y: iisy
                                            }
                                        };
                                        INX = iisx;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        data = {
            is_attacked: IS_ATTACKED,
            is_ships: IS_SHIPS,
            ships: SHIPS,
            index: INX
        };
        return data;
    },
    isShipsPlacedinMap: function(our, map, index, offset, rotate){
        var isShipsPlacedinMap = {
            i: 0,
            ii: 0,
            iix: 0,
            iiy: 0,
            iisx: 0,
            iisy: 0
        };
        var IS_ROTATE = rotate === undefined ? false : true;
        var ROTATE = IS_ROTATE ? rotate === 1 ? true : false : false;
        var RANGE = {
            x: 1,
            y: 1
        };
        if (offset.x !== undefined) {
            RANGE.x = offset.x;
        }
        if (offset.y !== undefined) {
            RANGE.y = offset.y;
        }
        if (ROTATE) {
            RANGE = {
                x: RANGE.y,
                y: RANGE.x
            };
        }
        for (isShipsPlacedinMap.ii = 0; isShipsPlacedinMap.ii < 5; isShipsPlacedinMap.ii++) {
            for (isShipsPlacedinMap.i = 1; isShipsPlacedinMap.i <= this.getShipsQ(our, (5 - isShipsPlacedinMap.ii)); isShipsPlacedinMap.i++) {
                var ats = this.getShipsPlaced(our, 5 - isShipsPlacedinMap.ii, isShipsPlacedinMap.i);
                if ((ats.index >= 0) && (ats.index < 100)) {
                    for (isShipsPlacedinMap.iix = 1; isShipsPlacedinMap.iix <= RANGE.x; isShipsPlacedinMap.iix++) {
                        for (isShipsPlacedinMap.iiy = 1; isShipsPlacedinMap.iiy <= RANGE.y; isShipsPlacedinMap.iiy++) {
                            var tmp = {
                                x: this.bigshipsObj[5 - isShipsPlacedinMap.ii][isShipsPlacedinMap.i].offset.x,
                                y: this.bigshipsObj[5 - isShipsPlacedinMap.ii][isShipsPlacedinMap.i].offset.y
                            };
                            tmp = ats.rotate === 0 ? {
                                x: tmp.x,
                                y: tmp.y
                            } : {
                                x: tmp.y,
                                y: tmp.x
                            };
                            for (isShipsPlacedinMap.iisx = 1; isShipsPlacedinMap.iisx <= tmp.x; isShipsPlacedinMap.iisx++) {
                                for (isShipsPlacedinMap.iisy = 1; isShipsPlacedinMap.iisy <= tmp.y; isShipsPlacedinMap.iisy++) {
                                    if ((index + (isShipsPlacedinMap.iix - 1) + ((isShipsPlacedinMap.iiy - 1) * 10)) === (ats.index + (isShipsPlacedinMap.iisx - 1) + ((isShipsPlacedinMap.iisy - 1) * 10))) {
                                        return true;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return false;
    },
    setFrame: function(obj, frm){
        var self = this;
        var scene = this.scene();
        scene.change(obj, {
            frame: frm
        });
    },
    selectBoard: function(obj, bef, mov, offset, size, pixel, boardoffset){
        size = size === undefined || size === null ? this.boardXY.size : size;
        pixel = pixel === undefined || pixel === null ? this.boardXY.pixelBig : pixel;
        boardoffset = boardoffset === undefined || boardoffset === null ? this.boardXY.offsetBig : boardoffset;
        mov = mov === undefined || mov === null ? 0 : mov;
        index = bef === undefined ? mov : bef + mov;
        offset = offset === null || offset === undefined ? {
            x: 1,
            y: 1
        } : offset;
        if ((mov !== -99) && (bef !== -99)) {
            index = this.isInvalidselectBoard(index, offset) ? bef : index;
            xy = this.selectBoardXY(index, size, pixel);
        }
        else {
            xy = {
                x: 320,
                y: 320
            };
            boardoffset = {
                x: 1,
                y: 1
            };
        }
        this.scene().change(obj, {
            x: xy.x + boardoffset.x,
            y: xy.y + boardoffset.y
        });
        return index;
    },
    selectBoardXY: function(index, size, pixel){
        return {
            x: Math.floor(index % size.x) * pixel.x,
            y: Math.floor(index / size.y) * pixel.y
        };
    },
    isInvalidselectBoard: function(mov, offset){
        return mov < 0 ? true : Math.floor(mov % 10) > 10 - offset.x ? true : mov > 99 - (10 * (offset.y - 1)) ? true : mov > 100 - offset.x ? true : false;
    },
    initSplash: function(){
        var self = this;
        var scene = this.scene();
        scene.defineSpritesheet('splash', app.resourceURL('splash.png'), 210, 133);
        scene.setLayerBackground(layer.splash, {
            sprite: 'splash',
            x: 0,
            y: 0,
            width: 210,
            height: 133
        });
    },
    initMenu: function(){
        var self = this;
        var scene = this.scene();
        scene.defineSpritesheet('menu', app.resourceURL('menu.png'), 178, 83);
        scene.setLayerBackground(layer.menu, {
            sprite: 'menu',
            x: 0,
            y: 0,
            width: 178,
            height: 83,
            x_src: 0,
            y_src: 0
        });
        animationTile.setup(scene);
        this.bgtilemenu = new (animationTile.animationTile)(layer.menubackground, 'bgtile.png', 0, 0, 40, 40, 4000);
        this.bgtilemenu.playAnimation();
        scene.defineSpritesheet('menustartgame', app.resourceURL('menu/menustartgame.png'), 136, 18);
        scene.defineSpritesheet('menuhowtoplay', app.resourceURL('menu/menuhowtoplay.png'), 136, 18);
        scene.defineSpritesheet('menuhighscore', app.resourceURL('menu/menuhighscore.png'), 136, 18);
        scene.defineSpritesheet('menucredit', app.resourceURL('menu/menucredit.png'), 136, 19);
        scene.defineSpritesheet('menuback', app.resourceURL('menu/menuback.png'), 136, 18);
        scene.defineSpritesheet('menustartgame_white', app.resourceURL('menu/menustartgame2.png'), 136, 18);
        scene.defineSpritesheet('menurestartgame', app.resourceURL('menu/menurestartgame2.png'), 136, 18);
        scene.defineSpritesheet('menuexit', app.resourceURL('menu/menuexit2.png'), 136, 18);
        this.menunewObj = scene.add({
            sprite: 'menustartgame',
            layer: layer.menuawalopts,
            frame: 0
        });
        this.menuhowtoplayObj = scene.add({
            sprite: 'menuhowtoplay',
            x: 0,
            y: 20,
            layer: layer.menuawalopts,
            frame: 1
        });
        this.menuhighscoreObj = scene.add({
            sprite: 'menuhighscore',
            x: 0,
            y: 40,
            layer: layer.menuawalopts,
            frame: 1
        });
        this.menucreditObj = scene.add({
            sprite: 'menucredit',
            x: 0,
            y: 60,
            layer: layer.menuawalopts,
            frame: 1
        });
        this.menuinstartgameObj = scene.add({
            sprite: 'menustartgame_white',
            x: 0,
            y: 0,
            layer: layer.menuingameopts,
            frame: 0
        });
        this.menuinrestartgameObj = scene.add({
            sprite: 'menurestartgame',
            x: 0,
            y: 20,
            layer: layer.menuingameopts,
            frame: 1
        });
        this.menuinexitObj = scene.add({
            sprite: 'menuexit',
            x: 0,
            y: 40,
            layer: layer.menuingameopts,
            frame: 1
        });
        this._selection = 0;
        this._state = 1;
        scene.changeLayer(layer.menuingameopts, {
            visible: false
        });
    },
    initGame: function(){
        var self = this;
        var scene = this.scene();
        this.boardXY = {
            size: {
                x: 10,
                y: 10
            },
            pixelBig: {
                x: 20,
                y: 20
            },
            pixelMini: {
                x: 8.5,
                y: 8
            },
            offsetBig: {
                x: 25,
                y: 25.5
            },
            offsetMini: {
                x: 234.4,
                y: 5.1
            }
        };
        this.ships = {
            our: {
                q: [0, 1, 1, 1, 1, 1],
                loc: {}
            },
            enemy: {
                q: [0, 3, 2, 2, 1, 1],
                loc: {}
            }
        };
        scene.defineSpritesheet('bgboard', app.resourceURL('board/bgboard.png'), 320, 320);
        scene.defineSpritesheet('bigboard', app.resourceURL('board/board.png'), 240, 240);
        scene.defineSpritesheet('bigships5x1', app.resourceURL('board/airshipcarrier.png'), 100, 100);
        scene.defineSpritesheet('bigships4x1', app.resourceURL('board/battleship.png'), 80, 80);
        scene.defineSpritesheet('bigships3x1', app.resourceURL('board/destroyer.png'), 60, 60);
        scene.defineSpritesheet('bigships2x1', app.resourceURL('board/cruiser.png'), 40, 40);
        scene.defineSpritesheet('bigships1x1', app.resourceURL('board/patrolboat.png'), 20, 20);
        scene.defineSpritesheet('miniboard', app.resourceURL('mini/board.png'), 84, 84);
        scene.defineSpritesheet('miniships5x1', app.resourceURL('mini/airshipcarrier.png'), 40, 40);
        scene.defineSpritesheet('miniships4x1', app.resourceURL('mini/battleship.png'), 32, 32);
        scene.defineSpritesheet('miniships3x1', app.resourceURL('mini/destroyer.png'), 24, 24);
        scene.defineSpritesheet('miniships2x1', app.resourceURL('mini/cruiser.png'), 16, 16);
        scene.defineSpritesheet('miniships1x1', app.resourceURL('mini/patrolboat.png'), 8, 8);
        scene.setLayerBackground(layer.bgboard, {
            sprite: 'bgboard',
            width: 320,
            height: 320
        });
        scene.defineSpritesheet('shoot', app.resourceURL('board/shoot.png'), 20, 20);
        scene.defineSpritesheet('minishoot', app.resourceURL('board/minishoot.png'), 8, 8);
        scene.defineSpritesheet('pointer', app.resourceURL('board/pointer_white.png'), 20, 20);
        for (ii = 0; ii < 5; ii++) {
            for (i = 0; i < 5; i++) {
                scene.remove(this.boxshipsObj[5 - ii][i + 1].obj, layer.boardships);
                scene.remove(this.bigshipsObj[5 - ii][i + 1].obj, layer.ships);
                scene.remove(this.minishipsObj[5 - ii][i + 1].obj, layer.miniships);
            }
        }
        this.boxshipsObj = {};
        this.bigshipsObj = {};
        this.minishipsObj = {};
        for (ii = 0; ii < 5; ii++) {
            var tempArraybigshipsObj = {};
            for (i = 0; i < 5; i++) {
                scene.remove(tempArraybigshipsObj[i + 1], layer.ships);
                tempArraybigshipsObj[i + 1] = {
                    obj: scene.add({
                        sprite: 'bigships' + (5 - ii) + 'x1',
                        x: 321,
                        y: 321,
                        layer: layer.ships,
                        frame: 0
                    }),
                    offset: {
                        x: (5 - ii),
                        y: 1
                    }
                };
            }
            this.bigshipsObj[5 - ii] = tempArraybigshipsObj;
        }
        for (ii = 0; ii < 5; ii++) {
            var tempArrayminishipsObj = {};
            for (i = 0; i < 5; i++) {
                tempArrayminishipsObj[i + 1] = {
                    obj: scene.add({
                        sprite: 'miniships' + (5 - ii) + 'x1',
                        x: 321,
                        y: 321,
                        layer: layer.shipssmall,
                        frame: 0
                    }),
                    offset: {
                        x: (5 - ii),
                        y: 1
                    }
                };
            }
            this.minishipsObj[5 - ii] = tempArrayminishipsObj;
        }
        for (i = 0; i < 100; i++) {
            scene.remove(this.shootObj[i], layer.shoot);
            scene.remove(this.minishootObj[i], layer.shoot);
        }
        delete this.shootObj;
        delete this.minishootObj;
        this.shootObj = {};
        this.minishootObj = {};
        for (i = 0; i < 100; i++) {
            this.shootObj[i] = scene.add({
                sprite: 'shoot',
                x: 321,
                y: 321,
                layer: layer.shoot,
                frame: 0
            });
            this.minishootObj[i] = scene.add({
                sprite: 'minishoot',
                x: 321,
                y: 321,
                layer: layer.shoot,
                frame: 0
            });
        }
        scene.remove(this.pointerObj[0], layer.pointer);
        this.pointerObj = {};
        this.pointerObj[0] = scene.add({
            sprite: 'pointer',
            x: 321,
            y: 321,
            layer: layer.pointer,
            frame: 0
        });
        scene.remove(this.bigboardObj, layer.board);
        scene.remove(this.miniboardObj, layer.board);
        this.bigboardObj = scene.add({
            sprite: 'bigboard',
            x: 0,
            y: 0,
            layer: layer.board,
            frame: 0
        });
        this.miniboardObj = scene.add({
            sprite: 'miniboard',
            x: 0,
            y: 0,
            layer: layer.board,
            frame: 0
        });
        scene.defineSpritesheet('legend', app.resourceURL('board/legend.png'), 16, 16);
        scene.remove(this.bigboardObj, layer.board);
        scene.remove(this.bigboardObj, layer.board);
        scene.remove(this.bigboardObj, layer.board);
        this.currentScoreObj = scene.add({
            sprite: 'legend',
            x: 0,
            y: 0,
            layer: layer.board,
            frame: 2
        });
        this.currentHealthObj = scene.add({
            sprite: 'legend',
            x: 0,
            y: 0,
            layer: layer.board,
            frame: 1
        });
        this.currentShootObj = scene.add({
            sprite: 'legend',
            x: 0,
            y: 0,
            layer: layer.board,
            frame: 0
        });
        font.setup(scene);
        delete this.currentScore;
        this.currentScore = new (font.TextObject)(scene, layer.text, 320 - ((320 - 235) / 2) - 2.5 - 25, 3 + 84 + 10, 0);
        this.currentScore.set(0);
        delete this.currentHealth;
        this.currentHealth = new (font.TextObject)(scene, layer.text, 320 - ((320 - 235) / 2) - 2.5 - 25, 3 + 84 + 10 + 14 + 10, 0);
        this.currentHealth.set(0);
        delete this.currentShoot;
        this.currentShoot = new (font.TextObject)(scene, layer.text, 320 - ((320 - 235) / 2) - 2.5 + 25, 3 + 84 + 10 + 14 + 10, 0);
        this.currentShoot.set(0);
        scene.defineSpritesheet('endikator', app.resourceURL('board/endikator.png'), 60, 25);
        scene.defineSpritesheet('icon', app.resourceURL('board/icon.png'), 50, 50);
        scene.remove(this.endikatorObj, layer.board);
        scene.remove(this.iconObj, layer.board);
        this.endikatorObj = scene.add({
            sprite: 'endikator',
            x: 0,
            y: 0,
            layer: layer.board,
            frame: 0
        });
        this.iconObj = scene.add({
            sprite: 'icon',
            x: 0,
            y: 0,
            layer: layer.board,
            frame: 0
        });
        messageBoard.setup(scene);
        delete this.currentMessage;
        this.currentMessage = new (messageBoard.TextObject)(scene, layer.gameover);
        this.currentMessage.set("gameover.win");
    },
    gameOver: function(who){
        var self = this;
        var scene = self.scene();
        if (who) {
            this.currentMessage.set("gameover.win");
        }
        else {
            this.currentMessage.set("gameover.lose");
        }
        this.currentMessage.show();
        setTimeout(function(){
            self.currentMessage.hide();
            self.setGameState(gameState.MENU);
        }, 3500);
        app.msg('setScore', {
            SCORE: this.getScore(who)
        });
    },
    ':active': function(){
    },
    ':inactive': function(){
    },
    c: function(a, b, c, d){
        a = a === undefined || a === null ? "" : a;
        b = b === undefined || b === null ? "" : b;
        c = c === undefined || c === null ? "" : c;
        d = d === undefined || d === null ? "" : d;
        console.log(a + " " + b + " " + c + " " + d);
    }
});
